package com.jerry.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class DiscoveryController {

    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/discovery/serviceList")
    public List<String> getServiceList() {
        List<String> serviceList = discoveryClient.getServices();
        for (String service : serviceList) {
            log.info("服务======" + service);
        }
        return serviceList;
    }

    @GetMapping("/discovery/serviceInstanceList/{service}")
    public List<ServiceInstance> getServiceInstanceList(@PathVariable("service") String service) {
        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances(service);
        for (ServiceInstance instance : serviceInstanceList) {
            log.info("服务实例======" + instance.getServiceId() +"   "+ instance.getHost() +"   "+instance.getPort() +"   "+ instance.getUri());
        }
        return serviceInstanceList;
    }
}
